/*     */ package net.sourceforge.appgen.model;
/*     */ 
/*     */ import java.io.Serializable;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;


import net.sourceforge.appgen.util.FileUtils;
/*     */ 
/*     */ public class SqlMap
/*     */   implements Serializable
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*     */   private Entity entity;
/*     */ 
/*     */   public SqlMap(Entity entity)
/*     */   {
/*  33 */     this.entity = entity;
/*     */   }
/*     */ 
/*     */   public String getFullPackageName() {
/*  37 */     return this.entity.getPackageName() + ".dao.maps";
/*     */   }
/*     */ 
/*     */   public String getResultMappingList() {
/*  41 */     return getResultMappingList("\t\t", false);
/*     */   }
/*     */ 
/*     */   public String getResultMappingListExceptLob() {
/*  45 */     return getResultMappingList("\t\t", true);
/*     */   }
/*     */ 
/*     */   public String getResultMappingList(String indent, boolean exceptLob) {
/*  49 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/*  51 */     for (Field field : this.entity.getFieldList()) {
/*  52 */       if ((!field.isCreate()) || (
/*  53 */         (exceptLob) && (field.isLob()))) continue;
/*  54 */       if (buffer.length() > 0) {
/*  55 */         buffer.append(FileUtils.ln());
/*     */       }
/*     */ 
/*  58 */       if (field.isAttachFileType())
/*  59 */         buffer.append(indent + "<result " + "column=\"" + field.getColumnName() + "\"" + " property=\"" + field.getFieldName() + ".name\"");
/*     */       else {
					if(field.getPkPosition()>0){
						buffer.append(indent + "<id " + "column=\"" + field.getColumnName() + "\"" + " property=\"" + field.getFieldName() + "\"");
					}else{
/*  61 */         buffer.append(indent + "<result " + "column=\"" + field.getColumnName() + "\"" + " property=\"" + field.getFieldName() + "\"");
					}
/*     */       }
/*     */ 
/*  64 */       if ("BLOB".equalsIgnoreCase(field.getColumnType())) {
/*  65 */         buffer.append(" jdbcType=\"BLOB\"");
/*     */       }
/*  67 */       if ("CLOB".equalsIgnoreCase(field.getColumnType())) {
/*  68 */         buffer.append(" jdbcType=\"CLOB\"");
/*     */       }
/*     */ 
/*  76 */       buffer.append("/>");
/*     */     }
/*     */ 
/*  81 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public String getPrimaryKeyParameterClassName() {
/*  85 */     if (this.entity.getPrimaryKeyFieldList().size() == 1) {
/*  86 */       String fieldType = ((Field)this.entity.getPrimaryKeyFieldList().get(0)).getFieldType();
/*     */ 
/*  88 */       if (String.class.getName().equals(fieldType)) {
/*  89 */         return "string";
/*     */       }
/*     */ 
/*  92 */       return fieldType;
/*     */     }
/*     */ 
/*  95 */     if (this.entity.getPrimaryKeyFieldList().size() >= 1) {
/*  96 */       return "java.util.Map";
/*     */     }
/*     */ 
/*  99 */     return "FIXME:";
/*     */   }
/*     */ 
/*     */   public String getInsert() {
/* 103 */     return getInsert("\t\t");
/*     */   }
/*     */ 
/*     */   public String getInsert(String indent) {
/* 107 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 109 */     buffer.append(indent + "INSERT INTO " + this.entity.getTableName() + FileUtils.ln());
/* 110 */     buffer.append(indent + "\t" + "(" + getAllColumnNames("", false) + ")" + FileUtils.ln());
/* 111 */     buffer.append(indent + "VALUES" + FileUtils.ln());
/* 112 */     buffer.append(indent + "\t" + "(" + getAllColumnVariables() + ")");
/*     */ 
/* 114 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public String getUpdate() {
/* 118 */     return getUpdate("\t\t");
/*     */   }
/*     */ 
/*     */   public String getUpdate(String indent) {
/* 122 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 124 */     buffer.append(indent + "UPDATE" + FileUtils.ln());
/* 125 */     buffer.append(indent + "\t" + this.entity.getTableName() + FileUtils.ln());
/* 126 */     buffer.append(indent + "SET" + FileUtils.ln());
/* 127 */     buffer.append(getAllColumnNamesAndVariablesExceptPrimaryKey(new StringBuilder(String.valueOf(indent)).append("\t").toString()) + FileUtils.ln());
/* 128 */     buffer.append(indent + "WHERE" + FileUtils.ln());
/* 129 */     buffer.append(getPrimaryKeyConjuction(indent + "\t"));
/*     */ 
/* 131 */     return buffer.toString();
/*     */   }
/*     */ 	
			public String getUpdateSelective() {
/* 118 */     return getUpdateSelective("\t\t");
/*     */   }

			public String getUpdateSelective(String indent) {
/* 122 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 124 */     buffer.append(indent + "UPDATE" + FileUtils.ln());
/* 125 */     buffer.append(indent + "\t" + this.entity.getTableName() + FileUtils.ln());
/* 127 */     buffer.append(indent+getUpdateSelectiveExceptPrimaryKey(new StringBuilder(String.valueOf(indent)).append("\t").toString()) + FileUtils.ln());
/* 128 */     buffer.append(indent + "WHERE" + FileUtils.ln());
/* 129 */     buffer.append(getPrimaryKeyConjuction(indent + "\t"));
/*     */ 
/* 131 */     return buffer.toString();
/*     */   }
			
			public String getInsertfist() {
				/* 118 */     return getInsertfist("\t\t");
				/*     */   }
			
			public String getInsertSecond() {
				/* 118 */     return getInsertSecond("\t\t");
				/*     */   }
			
			private String getInsertfist(String indent) {
				/* 270 */     StringBuffer buffer = new StringBuffer();
				/* 272 */     boolean first = true;
								List<Field> fl=entity.getFieldList();
				/* 273 */     for (int i=0;i<fl.size();i++) {
								buffer.append("\t\t<if test=\"").append(fl.get(i).getFieldName()).append(" != null");
								buffer.append(" \">\n");
				/* 279 */       buffer.append(indent + fl.get(i).getColumnName());
								if((i+1) < fl.size()){
									buffer.append(",\n\t\t");
								}else{
									buffer.append("\n\t\t");
								}
				/*     */ 		buffer.append("</if>\n");
				/* 281 */       first = false;
				/*     */     }
				/* 304 */     return buffer.toString();
				/*     */   }
			
			private String getInsertSecond(String indent) {
				/* 270 */     StringBuffer buffer = new StringBuffer();
				/* 272 */     boolean first = true;
								List<Field> fl=entity.getFieldList();
				/* 273 */     for (int i=0;i<fl.size();i++) {
								buffer.append("\t\t<if test=\"").append(fl.get(i).getFieldName()).append(" != null");
								buffer.append(" \">\n");
				/* 279 */       buffer.append(indent +"#{" + fl.get(i).getFieldName() + "}");
								if((i+1) < fl.size()){
									buffer.append(",\n\t\t");
								}else{
									buffer.append("\n\t\t");
								}
				/*     */ 		buffer.append("</if>\n");
				/* 281 */       first = false;
				/*     */     }
				/* 304 */     return buffer.toString();
				/*     */   }
			
			private String getUpdateSelectiveExceptPrimaryKey(String indent) {
				/* 270 */     StringBuffer buffer = new StringBuffer();
				/* 272 */     boolean first = true;
								buffer.append("<set>\n");
								List<Field> fl=entity.getFieldList();
				/* 273 */     for (int i=1;i<fl.size();i++) {
								buffer.append("\t\t<if test=\"").append(fl.get(i).getFieldName()).append(" != null ");
								if("java.lang.String".equals(fl.get(i).getFieldType())){
									buffer.append(" and ").append(fl.get(i).getFieldName()).append(" != ''");
					   	 		 }else{
					   	 			buffer.append(" and ").append(fl.get(i).getFieldName()).append(" != 0");
					   	 		 }
								buffer.append(" \">\n");
				/* 279 */       buffer.append(indent + fl.get(i).getColumnName() + " = " + "#{" + fl.get(i).getFieldName() + "}");
								if((i+1) < fl.size()){
									buffer.append(",\n\t\t");
								}else{
									buffer.append("\n\t\t");
								}
				/*     */ 		buffer.append("</if>\n");
				/* 281 */       first = false;
				/*     */     }
				/*     */ 	  buffer.append("\t</set>");
				/* 304 */     return buffer.toString();
				/*     */   }
				
/*     */   public String getDelete() {
/* 135 */     return getDelete("\t\t");
/*     */   }
/*     */ 
/*     */   public String getDelete(String indent) {
/* 139 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 141 */     buffer.append(indent + "DELETE FROM" + FileUtils.ln());
/* 142 */     buffer.append(indent + "\t" + this.entity.getTableName() + FileUtils.ln());
/* 143 */     buffer.append(indent + "WHERE" + FileUtils.ln());
/* 144 */     buffer.append(getPrimaryKeyConjuction(indent + "\t"));
/*     */ 
/* 146 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public String getSelect() {
/* 150 */     return getSelect("\t\t");
/*     */   }
/*     */ 
/*     */   public String getSelect(String indent) {
	/* 154 */     StringBuffer buffer = new StringBuffer();
	/*     */ 
	/* 156 */     buffer.append(indent + "SELECT" + FileUtils.ln());
				  buffer.append(indent+"<include refid=\"Base_Column_List\" />"+ FileUtils.ln());
	/* 158 */     buffer.append(indent + "FROM" + FileUtils.ln());
	/* 159 */     buffer.append(indent + "\t" + this.entity.getTableName() + FileUtils.ln());
	/* 160 */     buffer.append(indent + "WHERE" + FileUtils.ln());
	/*     */ 
	/* 162 */     buffer.append(getPrimaryKeyConjuction(indent + "\t"));
	/*     */ 
	/* 164 */     return buffer.toString();
	/*     */   }
/*     */ 
/*     */   public String getSelectList() {
/* 168 */     return getSelectList("\t\t");
/*     */   }
/*     */ 
/*     */   public String getSelectList(String indent) {
/* 172 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 174 */     buffer.append(indent + "SELECT" + FileUtils.ln());
/* 175 */      buffer.append(indent+"<include refid=\"Base_Column_List\" />"+ FileUtils.ln());
/* 176 */     buffer.append(indent + "FROM" + FileUtils.ln());
/* 177 */     buffer.append(indent + "\t" + this.entity.getTableName());
/*     */ 
/* 179 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public String getSelectListOrderByClause() {
/* 183 */     return getSelectListOrderByClause("\t\t");
/*     */   }
/*     */ 
/*     */   public String getSelectListOrderByClause(String indent) {
/* 187 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 189 */     buffer.append(indent + "ORDER BY" + FileUtils.ln());
/* 190 */     buffer.append(getOrderByClause(indent + "\t"));
/*     */ 
/* 192 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public String getSelectCount() {
/* 196 */     return getSelectCount("\t\t");
/*     */   }
/*     */ 
/*     */   public String getSelectCount(String indent) {
/* 200 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 202 */     buffer.append(indent + "SELECT" + FileUtils.ln());
/* 203 */     buffer.append(indent + "\t" + "COUNT(*)" + FileUtils.ln());
/* 204 */     buffer.append(indent + "FROM" + FileUtils.ln());
/* 205 */     buffer.append(indent + "\t" + this.entity.getTableName());
/*     */ 
/* 207 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   private String getAllColumnNamesExceptLOB(String indent) {
/* 211 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 213 */     for (Field field : this.entity.getFieldListExceptLob()) {
/* 214 */       if (buffer.length() > 0) {
/* 215 */         buffer.append("," + FileUtils.ln());
/*     */       }
/*     */ 
/* 218 */       buffer.append(indent + field.getColumnName());
/*     */     }
/*     */ 
/* 221 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   private String getAllColumnNames(String indent) {
/* 225 */     return getAllColumnNames(indent, true);
/*     */   }
/*     */ 
			public String getColumns(){
				String indent="\t\t";
				 return getAllColumnNames(indent, true);
			}
/*     */   private String getAllColumnNames(String indent, boolean ln) {
/* 229 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 231 */     for (Field field : this.entity.getFieldList()) {
/* 232 */       if (field.isCreate()) {
/* 233 */         if (buffer.length() > 0) {
/* 234 */           buffer.append(",");
/* 235 */           if (ln)
/* 236 */             buffer.append(FileUtils.ln());
/*     */           else {
/* 238 */             buffer.append(" ");
/*     */           }
/*     */         }
/*     */ 
/* 242 */         buffer.append(indent + field.getColumnName());
/*     */       }
/*     */     }
/*     */ 
/* 246 */     return buffer.toString();
/*     */   }
/*     */ 

/*     */   private String getAllColumnVariables() {
/* 250 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 252 */     for (Field field : this.entity.getFieldList()) {
/* 253 */       if (field.isCreate()) {
/* 254 */         if (buffer.length() > 0) {
/* 255 */           buffer.append(", ");
/*     */         }
/*     */ 
/* 258 */         if (field.isAttachFileType())
/* 259 */           buffer.append("#{" + field.getFieldName() + ".name}");
/*     */         else {
/* 261 */           buffer.append("#{" + field.getFieldName() + "}");
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 266 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   private String getAllColumnNamesAndVariablesExceptPrimaryKey(String indent) {
/* 270 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 272 */     boolean first = true;
/* 273 */     for (Field field : this.entity.getFieldList()) {
/* 274 */       if ((!field.isCreate()) || (field.getPkPosition() != 0) || 
/* 275 */         (field.isAttachFileType())) continue;
/* 276 */       if (!first) {
/* 277 */         buffer.append("," + FileUtils.ln());
/*     */       }
/* 279 */       buffer.append(indent + field.getColumnName() + " = " + "#{" + field.getFieldName() + "}");
/*     */ 
/* 281 */       first = false;
/*     */     }
/*     */ 
/* 286 */     if (first) {
/* 287 */       Iterator<Field> iteL= this.entity.getPrimaryKeyFieldList().iterator();
					if (iteL.hasNext()) { 
						Field field = (Field)iteL.next();
/* 288 */         buffer.append(indent + field.getColumnName() + " = " + "#{" + field.getFieldName() + "}");
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 293 */     for (Field field : this.entity.getFieldList()) {
/* 294 */       if ((!field.isCreate()) || (field.getPkPosition() != 0) || 
/* 295 */         (!field.isAttachFileType())) continue;
/* 296 */       buffer.append(FileUtils.ln());
/* 297 */       buffer.append(indent + "<isEqual property=\"" + field.getFieldName() + ".checkSaveOrDelete\" compareValue=\"true\" open=\",\">" + FileUtils.ln());
/* 298 */       buffer.append(indent + "\t" + field.getColumnName() + " = " + "#" + field.getFieldName() + ".name#" + FileUtils.ln());
/* 299 */       buffer.append(indent + "</isEqual>");
/*     */     }
/*     */ 
/* 304 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   private String getPrimaryKeyConjuction(String indent) {
/* 308 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 310 */     boolean first = true;
/* 311 */     for (Field field : this.entity.getPrimaryKeyFieldList()) {
/* 312 */       if (!first) {
/* 313 */         buffer.append(FileUtils.ln());
/*     */       }
/* 315 */       buffer.append(indent);
/* 316 */       if (!first) {
/* 317 */         buffer.append("AND ");
/*     */       }
/* 319 */       buffer.append(field.getColumnName() + " = " + "#{" + field.getFieldName() + "}");
/* 320 */       first = false;
/*     */     }
/*     */ 
/* 323 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   private String getOrderByClause(String indent) {
/* 327 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 329 */     for (Field field : this.entity.getPrimaryKeyFieldList()) {
/* 330 */       if (buffer.length() > 0) {
/* 331 */         buffer.append("," + FileUtils.ln());
/*     */       }
/*     */ 
/* 334 */       buffer.append(indent + field.getColumnName() + " ASC");
/*     */     }
/*     */ 
/* 337 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public Entity getEntity() {
/* 341 */     return this.entity;
/*     */   }
/*     */ 
/*     */   public void setEntity(Entity entity) {
/* 345 */     this.entity = entity;
/*     */   }
/*     */ }

/* Location:           C:\Users\lzl6066\Downloads\net.sourceforge.appgen_1.2.0\plugins\net.sourceforge.appgen_1.2.0.201002031718\
 * Qualified Name:     net.sourceforge.appgen.model.SqlMap
 * JD-Core Version:    0.6.0
 */